//
//  WJMianController.m
//  WJBSApp
//
//  Created by dllo on 16/7/18.
//  Copyright © 2016年 王景伟. All rights reserved.
//
// http://s.budejie.com/topic/list/jingxuan/1/budejie-android-6.4.3/0-20.json?
// http://s.budejie.com/topic/list/jingxuan/1/bs0315-iphone-4.2/0-20.json

#import "WJMianController.h"

@interface WJMianController ()
@property (nonatomic, strong) NSMutableArray *array;
///> 当加载下一页数据是需要这个参数
@property (nonatomic,   copy) NSNumber *np;
@property (nonatomic, strong) WJMainTableView *tableView;
@end

@implementation WJMianController

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[WJVideoPlayView sharedPlayView] resetAVPlayer];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createView];
}

- (void)createView {
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.array = [NSMutableArray array];
    self.tableView = [WJMainTableView mainTableViewWithFrame:CGRectMake(0, 0, WIDTH, self.view.height) controller:self];
    [self.view addSubview:self.tableView];
    
    __block typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf loadNewData];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadMoreData];
    }];
    
    [self.tableView.mj_header beginRefreshing];
}


- (void)loadNewData {
    NSString *str1 = [self.url stringByReplacingOccurrencesOfString:@"http://" withString:@""];
    NSString *str2 = [str1 stringByReplacingOccurrencesOfString:@".budejie.com/topic/" withString:@""];
    NSString *str3 = [str2 stringByReplacingOccurrencesOfString:@"/bs0315-iphone-4.2/0-20.json" withString:@""];
    NSString *str4 = [str3 stringByReplacingOccurrencesOfString:@"/" withString:@""];
    NSString *str5 = [NSString stringWithFormat:@"%@.plist",str4];
    NSString *str = [KESHAN stringByAppendingPathComponent:str5];
    [WJNetTool GET:self.url body:nil header:nil response:WJJSON success:^(id resuposeObject) {
        [resuposeObject writeToFile:str atomically:YES];
        NSDictionary *dic = resuposeObject;
        if (dic[@"info"][@"np"] != nil) {
            self.np = dic[@"info"][@"np"];
        }
        [self.array removeAllObjects];
        NSArray *array = [WJBSTool getSafeArray:dic[@"list"]];
        for (NSDictionary *dic in array) {
            WJMainModel *model = [WJMainModel modelWithJSON:dic];
            WJMianLayout *layout = [[WJMianLayout alloc]initWithModel:model];
            [self.array addObject:layout];
        }
        self.tableView.dataArr = self.array;
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSError *error) {
        //显示加载失败的信息
        [SVProgressHUD showErrorWithStatus:@"链接失败,读取本地数据"];
        // 列表大字典
        NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:str];
        self.np = dic[@"info"][@"np"];
        [self.array removeAllObjects];
        NSArray *array = [WJBSTool getSafeArray:dic[@"list"]];
        for (NSDictionary *dic in array) {
            WJMainModel *model = [WJMainModel modelWithJSON:dic];
            WJMianLayout *layout = [[WJMianLayout alloc]initWithModel:model];
            [self.array addObject:layout];
        }
        self.tableView.dataArr = self.array;
        [self.tableView.mj_header endRefreshing];
    }];
}




- (void)loadMoreData {
    [self.tableView.mj_header endRefreshing];
    if ([self.np isKindOfClass:[NSNull class]] || self.np == nil) {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
        return;
    }
    NSString *oldStr = [NSString stringWithFormat:@"%@-20.json",self.np];
    NSString *str4 = [self.url stringByReplacingOccurrencesOfString:@"0-20.json" withString:oldStr];
    [WJNetTool GET:str4 body:nil header:nil response:WJJSON success:^(id resuposeObject) {
        NSDictionary *dic = resuposeObject;
        self.np = dic[@"info"][@"np"];
        NSArray *array = [WJBSTool getSafeArray:dic[@"list"]];
        for (NSDictionary *dic in array) {
            WJMainModel *model = [WJMainModel modelWithJSON:dic];
            WJMianLayout *layout = [[WJMianLayout alloc]initWithModel:model];
            [self.array addObject:layout];
        }
        self.tableView.dataArr = self.array;
        [self.tableView.mj_footer endRefreshing];
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"加载失败"];
    }];
}

@end

